<include file="public:header" /> <include file="public:left" />
<style>
    .add_img .img_case{width: 100px;height: 100px;border:1px solid #000;margin: 0 10px 10px 0;}
    .add_img .img_case img{width: 100%;height:100%;}
    .add_img .img_case:last-child .ld_close{display:none}
    .add_img{display: flex;flex-wrap:wrap;margin: 15px 0 0 60px}
    .sku-atom .textbox .textbox_295{margin:5px 0;}

    .table-sku-stock tr td{border: 1px #139667 solid;margin:5px 5px;padding:10px 10px;}
    .table-sku-stock thead tr th{border: 1px #139667 solid;margin:5px 5px;padding:10px 10px;}
    #param-items tr td{margin-top: 10px;}
    #input-attrvalue{position: fixed;z-index: 1010;border-radius: 2px;-webkit-box-shadow: 0px 1px 6px rgba(0,0,0,0.2);box-shadow: 0px 1px 6px rgba(0,0,0,0.2);}
</style>
	<if condition="$id">	
			<h2><strong>编辑商品</strong>
				<a class="fr" href="{:url('Goods/lists')}">返回商品列表</a>
			</h2>
			<ul class="ulColumn2">
				<li>
					<span class="item_name" style="width:120px;">商品标题：</span>
					{$goods_result.name}
				</li>
				<li>
					<span class="item_name" style="width:120px;">商品操作：</span>
					<if condition="$goods_result.status eq 1">上架<else />下架</if>
				</li>
				<li>
	              <span class="item_name" style="width:120px;">排序：</span>
	              {$goods_result.sort}
	            </li>
				<li>
					<span class="item_name" style="width:120px;">商品大图：</span>
					<img src="{$config_uploads}{$goods_result.top_image}" width="100" height="100">
				</li>
				<li>
					<span class="item_name" style="width:120px;">商品主图：</span>
					<img src="{$config_uploads}{$goods_result.image}" width="100" height="100">
				</li>
				<li>
				  <span class="item_name" style="width:120px;">其他图：</span>
				  <div class="add_img" >
				      <if condition="$goods_result['images']">
				          <volist name="goods_result.images" id="vo" key="k">
				              <div class="img_case" style="position: relative">
				                  <div class="ld_close" style="border-radius: 100px;padding:3px 5px 3px 5px;border: 1px solid red;background: red;color: #fff;position: absolute;z-index: 9999;top:-8px;right: -5px">
				                      x
				                  </div>
				                  <img src="__STATIC__/uploads/{$vo}" class="photoup" />
				                  <input type="hidden" name="photo[]" value="{$vo}"/>
				              </div>
				          </volist>
				      </if>
				  </div>
				</li>
				<li>
					<span class="item_name" style="width:120px;">商品规格：</span>
					<!-- 商品库存 -->
					<div class="js-goods-stock control-group" style="display:<notempty name="option_category_result">block<else/>none</notempty>;margin: 15px 0 0 60px;" >
						<table class="table-sku-stock">

						<notempty name="option_category_result">
						<thead>
							<tr>
								<th>{$option_category_result[0]['title']?$option_category_result[0]['title']:'颜色'}</th>
								<notempty name="attrvalue[0]['title']"><th>{$option_category_result[1]['title']}</th></notempty>
								<th>成本价格（元）</th>
								<th>价格（元）</th>
								<th>库存</th>
							</tr>
						</thead>
						<tbody>
						<?php $i=0;$f=0;$t=0;?>
						<foreach name="goods_option_value_result" key="key" id="attrv">
						<tr>
							<?php if(isset($attrvalue)){?>
							<?php if(count($attrvalue) < 1){?>
								<td rowspan="{$attrvalue_count}">{$attrname[$f]['title']}
								      <input type="hidden" name="attrname[]" value="{$attrname[$f]['title']}">
								</td>
								<?php $f++;?>
							<?php }else{?>
								<?php if($i == 0 || $i == $attrvalue_count):?>
									<td rowspan="{$attrvalue_count}">{$attrname[$f]['title']}
								      <input type="hidden" name="attrname[]" value="{$attrname[$f]['title']}">
								    </td>
								    <?php $i=0;?>
								    <if condition="$f<count($attrname)-1">
										<?php $f++;?>
									</if>
								<?php endif;?>
							<?php }?>
							<?php }else{?>
								<td rowspan="1">{$attrname[$f]['title']}
								      <input type="hidden" name="attrname[]" value="{$attrname[$f]['title']}">
								</td>
								<?php $f++;?>
							<?php }?>
							<notempty name="attrvalue">
								<?php if($t == $attrvalue_count):?>
									<?php $t=0;?>
								<?php endif;?>
								<td>
									{$attrvalue[$t]['title']}
									<input type='hidden' name="attrval[]" value="{$attrvalue[$t]['title']}"/>
								</td>
								<?php $t++;?>
							</notempty>
							<?php $i++?>
							<td>{$attrv['oldprice']}</td>
							<td>{$attrv['price']}</td>
							<td>{$attrv['stock']}</td>
						</tr>
						</foreach>
						</tbody>
						</notempty>
						<!-- <tfoot>
							<tr>
								<td colspan="6">
									<div class="batch-opts">
										批量设置：
										<span class="js-batch-type">
											<a class="js-batch-price" href="javascript:;" onclick="showBatch(this, 'price')">价格</a>
											&nbsp;&nbsp;
											<a class="js-batch-stock" href="javascript:;" onclick="showBatch(this, 'store')">库存</a>
										</span>
										<span class="js-batch-form" style="display: none;">
											<input type="text" class="js-batch-txt input-mini" placeholder="">
											<a class="js-batch-save" href="javascript:;">保存</a>
											<a class="js-batch-cancel" href="javascript:;">取消</a>
											<p class="help-desc"></p>
										</span>
									</div>
								</td>
							</tr>
						</tfoot> -->
						</table>
						<div class="clear"></div>
					</div>
					<!-- 商品库存 -->
				</li>
				<li style="height:auto;">
					<span class="item_name" style="width:130px;">商品运费：</span>{$goods_result.postage}元
				</li>
				<li style="height:auto;">
					<span class="item_name" style="width:130px;">简介（80字以内）：</span><br>
					<textarea name="des" id="" style="margin-left: 150px" cols="60" rows="5" disabled>{$goods_result.des|htmlspecialchars_decode}</textarea>
				</li>
				<li style="height:auto;">
				  <span class="item_name" style="width:120px;">内容：</span>
				  <script id="editor" name="content" type="text/plain" style="width:700px;height:300px;margin-left:125px; margin-top:-12px;">{$goods_result.content|htmlspecialchars_decode}</script>
				</li>
			</ul>
			<div style="height: 150px;"></div>
	<else />
			<h2><strong>添加商品</strong>
				<a class="fr" href="{:url('Goods/lists')}">返回商品列表</a>
			</h2>
			<ul class="ulColumn2">
				<li>
					<span class="item_name" style="width:120px;">商品标题：</span>
					<input type="text" name="name"  class="textbox textbox_295" value=""/>
				</li>
				<li>
					<span class="item_name" style="width:120px;">商品分类：</span>
				
					<select name="cid">
						<option value="0">请选择商品分类</option>
						<volist name="goods_category" id="vo">
							<option value="{$vo.id}">{$vo.name}</option>
						</volist>
					</select>
				</li>
				<li>
	              <span class="item_name" style="width:120px;">排序：</span>
	              <input type="text" class="textbox textbox_295" name="sort" value=""/>
	            </li>
	            <li>
				  <span class="item_name" style="width:120px;">属性：</span>   <a href="javascript:;" id='add-param' onclick="addParam2()" style="margin-top:10px;" class="btn btn-primary"  title="添加属性"><i class='fa fa-plus'></i> 添加属性</a>

				  <table style="margin-left: 60px;margin-top: 10px;" cellspacing="100" >
					  <tbody id="param-items" >
					  </tbody>
				  </table>
				</li>
				<li>
					<span class="item_name" style="width:120px;">商品大图上传：</span>
					<div class="textbox textbox_295" style="width:200px;display: inline-block;">
						<input type="file" name="goods_top_image" />
					</div>
				</li>
				<li>
					<span class="item_name" style="width:120px;">商品主图上传：</span>
					<input type="file" name="goods_image" class="textbox textbox_295" />
				</li>
				<li>
				  <span class="item_name" style="width:120px;">其他图：</span>

				  <div class="add_img" >
				      <div class="img_case" style="position: relative">
				          <img src="__STATIC__/admin/images/add.png" class="photoup" />
				          <input type="file" name="photoname[]" class="photoname" style="display:none;"/>
				      </div>
				  </div>
				</li>
				<li>
					<span class="item_name" style="width:120px;">商品规格：</span>
					<div class="js-goods-sku control-group" style="margin: 15px 0 0 60px;">
					    <div id="sku-region" class="controls left" style="width:560px;display: inline-block;">

					        <div class="sku-group">
					            <div class="js-sku-list-container">
									<div class="js-sku-group-opts sku-sub-group">
										<h3 class="sku-group-title">
											<button type="button" onclick="addAttrTitle(this)" class="js-add-sku-group btn btn-default" style="width:100px;height:25px">添加规格项目</button>
										</h3>
									</div>
									<input type="hidden" name="delattrname" id="delattrname" value="" />
									<input id="which-attr" type="hidden" value="" />
									<input id="batch-type" type="hidden" value="" />
								</div>
					    	</div>
							<div class="clear"></div>
						</div>
					</div>
					<div class="js-goods-stock control-group" style="margin: 15px 0 0 60px;" style="display:<notempty name="option_category_result">block<else/>none</notempty>;">
						<table class="table-sku-stock">

						</table>
					</div>
				</li>
				<li style="height:auto;">
					<span class="item_name" style="width:130px;">商品运费：</span>
					<input type="text" name="postage" class="form-control js-price input-mini textbox textbox_295" onkeypress="return isDecimal(this, 2, false, event);" onpaste="return false" oncontextmenu="return false;" value=""/>(元)
				</li>
				<li style="height:auto;">
					<span class="item_name" style="width:130px;">简介（80字以内）：</span><br>
					<textarea name="des" id="" style="margin-left: 150px" cols="60" rows="5"></textarea>
				</li>
				<li style="height:auto;">
				  <span class="item_name" style="width:120px;">内容：</span>
				  <script id="editor" name="content" type="text/plain" style="width:700px;height:300px;margin-left:125px; margin-top:-12px;"></script>
				</li>
			</ul>

	</if>
<!--属性值添加框-->
<div id="input-attrvalue" class="popover bottom popover-chosen-wrap" style="display:none;">
	<div class="arrow"></div>
	<div class="popover-inner popover-chosen">
		<div class="popover-content" style="height:30px">
			<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input select2-default select2-search-field" tabindex="-1" style="width: 200px;font-size:16px;height: 100%;" />
			<button type="button" class="btn btn-primary js-btn-confirm" data-loading-text="确定" onclick="addAttrVal(this)" style="height:35px">确定</button>
			<button type="button" class="btn btn-default js-btn-cancel" onclick="hideAttrValPannel(this)" style="height:35px">取消</button>
		</div>
	</div>
</div>
<script type="text/javascript" src="__STATIC__/admin/js/jquery-ui-1.10.3.min.js"></script>
<script type="text/javascript">
	//点击检测是否填写价格
	$(".link_btn").click(function(){
		//检测成本价
		if(!is_input($("input[name='attroldprice[]']"))){
			alert('请填写成本价');
			return false;
		}

		//检测价格
		if(!is_input($("input[name='attrprice[]']"))){
			alert('请填写商品价格');
			return false;
		}

		//检测是否填写库存
		if(!is_input($("input[name='attrtotal[]']"))){
			alert('请填写商品库存');
			return false;
		}

		//检测是否选择商品分类
		var cat_name_val = $("select[name='cid']").val();
		if(cat_name_val == '0'){
			alert('请选择商品分类');
			return false;
		}

		//检测是否选择商品规格
		var attrname_val = $("input[name='attrname[]']").val();
		if(typeof attrname_val == "undefined"){
			alert('请选择商品规格');
			return false;
		}

		//检测运费数据是否规范
		var postage_val = $("input[name='postage']").val();
		var r = /^[-+]?\d*$/;
		if(!r.test(postage_val)){
			alert('运费请填写整数');
			return false;
		}

		//检测是否填写商品标题
		var name_val = $("input[name='name']").val();

		if(name_val == ""){
			alert('请填写商品标题');
			return false;
		}
	});

	function is_input(obj){
		var bool = true;
		obj.each(function(){
			var val = $(this).val();
			if(val == ''){
				bool = false;
				$(this).focus();
				return false;
			}
		});

		return bool
	}

	$(function() {
        $("#param-items").sortable({handle: '.fa-move'});
    })
</script>
<script type="text/javascript">


    function addParam2() {
        $.ajax({
            "url": "{:url('goods/tpl')}",
            success: function(data) {
                $('#param-items').append(data);
            }
        });
        return;
    }

    //
    function deleteParam1(o) {
        $.ajax({
            "url": "{:url('goods/tpl')}",
            success: function(data) {
                $(o).parent().parent().remove();
            }
        });
        return;

    }

	function PreviewImage(imgFile) {

        var filextension = imgFile.value.substring(imgFile.value.lastIndexOf("."), imgFile.value.length);
        filextension = filextension.toLowerCase();
        if ((filextension != '.jpg') && (filextension != '.gif')
                && (filextension != '.jpeg') && (filextension != '.png')
                && (filextension != '.bmp')) {
            alert("对不起，系统仅支持标准格式的照片，请您调整格式后重新上传，谢谢 !");
            imgFile.focus();
        } else {
            var path;
            path = window.URL.createObjectURL(imgFile.files[0]);// FF 7.0以上
            $(imgFile).parent(".img_case").append('<div class="ld_close" style="border-radius: 100px;padding:3px 5px 3px 5px;border: 1px solid red;background: red;color: #fff;position: absolute;z-index: 9999;top:-8px;right: -5px">x</div>');

            $(imgFile).siblings("img").attr("src",path);
            //   alert($(imgFile).parent(".img_case").html());
            $(".add_img").append('<div class="img_case"  style="position: relative"><div class="ld_close" style="border-radius: 100px;padding:3px 5px 3px 5px;border: 1px solid red;background: red;color: #fff;position: absolute;z-index: 9999;top:-8px;right: -5px">x</div> <img src="__STATIC__/admin/images/add.png" class="photoup" /><input type="file" name="photoname[]" class="photoname" style="display:none;"/></div>');
        }
    }

    $(document).on("change",'.photoname',function(){
        PreviewImage(this);
    });

    //上传图片
    $(document).on("click",'.photoup',function(){
        $(this).siblings("input").trigger("click");
    });

    //删除图片
    $(document).on("click",'.ld_close',function(){
        if(confirm('确定删除图片吗？')){
            $(this).parent('.img_case').remove()
        }
    });

    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    var ue = UE.getEditor('editor');

    function isFocus(e) {
        alert(UE.getEditor('editor').isFocus());
        UE.dom.domUtils.preventDefault(e)
    }

    function setblur(e) {
        UE.getEditor('editor').blur();
        UE.dom.domUtils.preventDefault(e)
    }

    function insertHtml() {
        var value = prompt('插入html代码', '');
        UE.getEditor('editor').execCommand('insertHtml', value)
    }

    function createEditor() {
        enableBtn();
        UE.getEditor('editor');
    }
</script>
<!-- 商品属性数据 -->
<script>
//属性全局数组
var attrArr = [];
attrArr["key"] = [];
attrArr["value"] = [];

//属性标题
<?php
if(isset($option_category_result)){
	$a = array();
	foreach($option_category_result as $key=>$val){
		$a[]=$val['title'];
	}
	echo 'attrArr["key"] = '.json_encode($a).';';
}	
?>


//属性一
<?php 
if(isset($attrname)){
	$attrNames=array();
	foreach ($attrname as $key => $val) {
		$nameIds[]=$val['id'];
		$attrNames[]=$val['title'];
	}
	echo 'attrArr["value"][0] = '.json_encode($attrNames).';';
	echo 'var nameIds = "' .implode(',', $nameIds).'";';
	echo '$(function () {
			$("#delattrname").val(nameIds);
		});';
}
?>

//属性二
<?php
if(isset($attrvalue)){
	$attrVals=array();
	foreach($attrvalue as $key=>$val){
		$attrVals[]=$val['title'];
	}
	$attrVals=array_unique($attrVals);
	echo 'attrArr["value"][1] = '.json_encode($attrVals).';';
}
?>



//添加属性
function addAttrTitle(obj) {
	var attrNum = $(".attr-title").length;
	if(attrNum < 2) {
		var html = '<div class="sku-sub-group attr-title">';
		html += '<h3 class="sku-group-title">';
		html +='<div class="col-lg-5">';

		if(attrNum < 1) {
			html += '<select name="attrtitleone" class="form-control" onchange="attrchange(this)" initializeAttr="no">';
		} else {
			html += '<select name="attrtitletow" class="form-control" onchange="attrchange(this)" initializeAttr="no">';
		}
		html +='</select>';
		html +='<a href="javascript:void(0)" onclick="rmAttrTitle(this)" style="display: inline-block;margin-left: 15px;font-size: 18px;color:black">×</a>';
		html +='</div>';


		html += '</h3>';
		html += '<div class="js-sku-atom-container sku-group-cont">';
		html += '<div>';
		html += '<div class="js-sku-atom-list sku-atom-list"></div>';
		html += '<a href="javascript:;" class="js-add-sku-atom add-sku" onclick="showAttrValPannel(this)">+添加</a>';
		html += '</div>';   
		html += '</div>';
		
		$(html).insertBefore($(obj).parent().parent());
		
		if(attrNum < 1) {
			initializeAttr("select[name='attrtitleone']");
		} else {
			initializeAttr("select[name='attrtitletow']");

		}
		//隐藏添加属性按钮
		if($(".attr-title").length >= 2) {
			$(".js-sku-group-opts").hide();
		}
		
		//显示表格
		if($(".attr-title").length > 0) {
			$(".js-goods-stock").show();
		}
		
		//数组增加属性
		attrArr["key"][attrNum] = "";
		
		$(".totalgoods").hide();
		
		//生成表格
		$(".table-sku-stock").html(createTable(attrArr));
		attrtotalonbin();
	} else {
		alert("只能添加两个规格项目！");
	}
}

//删除属性
function rmAttrTitle(obj) {
	var attrname = $(obj).parent().find("select").attr('name');
	if(attrname == 'attrtitleone'){
		$(".attr-title").remove();
		//数组减少属性
		attrArr["key"]=[];
		attrArr["value"] = [];
	}else{
		$(obj).parent().parent("h3").parent("div").remove();
		$("#select2-drop").hide();
		//数组减少属性
		attrArr["key"].splice(1,1);
		attrArr["value"].splice(1,1);
	}

	//console.log(attrArr);
	
	//少于两个属性
	if($(".attr-title").length < 2) {
		$(".js-sku-group-opts").show();
	}
	//隐藏表格
	if($(".attr-title").length < 1) {
		$(".js-goods-stock").hide();
		$(".totalgoods").show();
	}
	//生成表格
	$(".table-sku-stock").html(createTable(attrArr));
	attrtotalonbin();
}

//商品属性类型切换
function attrchange(obj){
	var name = $(obj).attr('name');

	var attrTitle = $(obj).find("option:selected").val();

	if(attrArr["key"].length > 1 && $.inArray(attrTitle, attrArr["key"]) > -1) {
		alert("两个商品规格名称不能相同！");
		return false;
	}

	if(name == 'attrtitletow'){
		attrArr["key"][1] = attrTitle;
	}else{
		attrArr["key"][0] = attrTitle;
	}
	//生成表格
	$(".table-sku-stock").html(createTable(attrArr));
	attrtotalonbin();
}

//初始商品属性标题
initializeAttr("select[name='attrtitleone']");
initializeAttr("select[name='attrtitletow']");

function initializeAttr(obj){
	var initattr = $(obj).attr('initializeAttr');
	if(initattr == 'yes'){
		return false;
	}

	//生成选择框下拉列表
	var property = <?php echo json_encode(array('种类', '尺寸','颜色','尺码','容量','规格','净重','上市时间','系列','机芯','适用','净含量','包装','款式','口味','产地','种类','内存','套餐','出行日期','出行人群','入住时间','房型','介质','版本','有效期','批量','其他'));?>;

	var html = '';
	var i;

	var olevalue = $(obj).val();
	
	for(i in property) {
		if(olevalue == property[i] ){
			html += '<option selected="selected" valuel="'+property[i]+'">'+property[i]+'</option>';
		}else{
			html += '<option valuel="'+property[i]+'">'+property[i]+'</option>';
		}
	}
	var fhtml = "<option valuel='0'>请先选择商品规格</option>";
	$(obj).html(fhtml+html);
	$(obj).attr('initializeAttr','yes');
}


//显示添加属性值面板
function showAttrValPannel(obj) {
	//判断有没有选择商品规格
	if($(obj).parent().parent().parent().find('select[name="attrtitleone"]').val() == '请先选择商品规格'){
		alert('请先选择商品规格');
		return false;
	}

	if($(obj).parent().parent().parent().find('select[name="attrtitletow"]').val() == '请先选择商品规格'){
		alert('请先选择商品规格');
		return false;
	}

	if($(obj).parent().parent().parent().find('select[name="attrtitleone"]').val()){
		$("#input-attrvalue").attr('option_name','option_one');
	}else{
		$("#input-attrvalue").attr('option_name','option_two');
	}

	$("#input-attrvalue").show().css({"top":($(obj).offset().top+$(obj).height()),"left":$(obj).offset().left-$("#input-attrvalue").width()/2+$(obj).width()/2});
	var attr_index = $(obj).parent().parent().parent().index();

	$("#which-attr").val(attr_index);
}


//商品规格库存变动， 总库存也变动
$(function () {
	$(".js-stock-num").change(function () {
		var total = 0;
		$(".js-stock-num").each(function () {
			total += parseInt($(this).val());
		});
		$("#total").val(total);
	});

	$("input[name ^='attrtotal['").on('blur',function(){
		var total = 0;
		$("input[name ^='attrtotal['").each(function(){
			total += $(this).val() ? parseInt($(this).val()) : 0;
		});
		$("#total").val(total);
	});
});


//添加属性活着更改属性都绑定属性total失去焦点
function attrtotalonbin(){
	$("input[name ^='attrtotal['").on('blur',function(){
		var total = 0;
		$("input[name ^='attrtotal['").each(function(){
			total += $(this).val() ? parseInt($(this).val()) : 0;
		});
		$("#total").val(total);
	});
}


//变换数组
function transArr(arr) {
	var trans_arr = [];
	trans_arr["key"] = [];
	trans_arr["value"] = [];
	
	var p,q;
	var i=0;
	for(p in arr) {
		trans_arr["key"][i] = p;
		for(q in arr[p]) {
			trans_arr["value"][i] = arr[p];
		}
		i++;
	}
	return trans_arr;
}

	
//生成表格
function createTable(arr) {
	//表格标题
	var thead = '<thead>';
	thead += '<tr>';
	var k;

	for(k in arr["key"]) {
		if((arr["value"][0] != undefined && arr['key'][k] != '' && arr['value'][k] != undefined) && arr['value'][k].length >0) {
			thead += '<th>'+arr["key"][k]+'</th>';
		}
	}
	thead += '<th class="th-price">成本价格（元）</th>';
	thead += '<th class="th-price">价格（元）</th>';
	thead += '<th class="th-stock">库存</th>';
	// thead += '<th class="th-code">商家编码</th>';
	thead += '</tr>';
	thead += '</thead>';
	
	//表格主体
	var i,j;
	var tbody = '<tbody>';
	
	var tbody_other = '<td>';
	tbody_other += '<input type="text"  name="attroldprice[]"  value="" class="form-control js-price input-mini" onkeypress="return isDecimal(this, 2, false, event);" onpaste="return false" oncontextmenu="return false;"/>';
	tbody_other += '</td>';
	tbody_other += '<td>';
	tbody_other += '<input type="text"  name="attrprice[]"  value="" class="form-control js-price input-mini" onkeypress="return isDecimal(this, 2, false, event);" onpaste="return false" oncontextmenu="return false;"/>';
	tbody_other += '</td>';

	tbody_other += '<td>';
	tbody_other += '<input type="text"  name="attrtotal[]"  value="" class="form-control js-stock-num input-mini" onkeypress="return isDecimal(this, 2, false, event);" onpaste="return false" oncontextmenu="return false;"/>';
	tbody_other += '</td>';

	// tbody_other += '<td>';
	// tbody_other += '<input type="text"  name="attrgoodssn[]"  value="" class="form-control js-code input-small" />';
	// tbody_other += '</td>';
	
	for(i in arr['value'][0]) {
	
		if((arr['key'][1] && arr['value'][1] != undefined && arr['key'][1] != '') && arr['value'][1].length>0) {
			
			for(j in arr['value'][1]) {
				tbody += '<tr>';
				
				if(j == 0) {
					tbody += '<td rowspan="'+arr['value'][1].length+'">';
					tbody += arr['value'][0][i];
					tbody += '<input type="hidden" value="'+arr['value'][0][i]+'" name="attrname[]"/>';
					tbody += '</td>';
				}
				
				tbody += '<td>';
				tbody += arr['value'][1][j];
				tbody += '<input type="hidden" name="attrval[]" value="'+arr['value'][1][j]+'" />';
				tbody += '</td>';
				tbody += tbody_other;
				tbody += '</tr>';
			}
		} else {
			
			tbody += '<tr>';
			tbody += '<td>';
			tbody += arr['value'][0][i];
			tbody += '<input type="hidden" value="'+arr['value'][0][i]+'" name="attrname[]"/>';
			tbody += '</td>';
			tbody += tbody_other;
			tbody += '</tr>';
		}
	}
	tbody += '</tbody>';

	// var tfoot = '<tfoot>';
	// tfoot += '<tr>';
	// tfoot += '<td colspan="6">';
	// tfoot += '<div class="batch-opts">';
	// tfoot += '批量设置：';
	// tfoot += '<span class="js-batch-type">';
	// tfoot += '<a class="js-batch-price" href="javascript:;" onclick="showBatch(this, \'price\')">价格</a>';
	// tfoot += '&nbsp;&nbsp;';
	// tfoot += '<a class="js-batch-stock" href="javascript:;" onclick="showBatch(this, \'store\')">库存</a>';
	// tfoot += '</span>';
	// tfoot += '</div>';
	// tfoot += '</td>';
	// tfoot += '</tr>';
	// tfoot += '</tfoot>';
	
	var html = '';
	html += thead;
	html += tbody;
	//html += tfoot;
	
	return html;
}


</script>
<!-- 商品属性数据 -->

<!-- 添加属性操作 -->
<script>

//隐藏添加属性值面板
function hideAttrValPannel(obj) {
	$("#input-attrvalue").hide();
}

//添加属性值至页面
function addAttrVal(obj) {
	var attrValue = $(obj).prev().val();
	var option_name = $(obj).parent().parent().parent().attr('option_name');

	if(attrValue == '') {
		return false;
	}
	var atom_index = $("#which-attr").val();
	if($.inArray(attrValue, attrArr["value"][atom_index]) > -1) {
		alert("请不要重复添加属性值！");
		return false;
	}
	
	var atom_id = ((attrArr["value"][atom_index] != undefined)? attrArr["value"][atom_index].length : 0);
	
	//添加属性值
	var html = '<div class="sku-atom textbox textbox_295" style="margin:5px 0">';
	html += '<span>'+attrValue+'</span>';
	html += '<a href="javascript:void(0)" class="close-modal small js-remove-sku-atom" onclick="rmAttrVal(this, \''+atom_index+'\')" style="float:right;font-size:20px;color:black">×</a>';
	html += '<input type="hidden" name="'+option_name+'[]" value='+attrValue+'>';
	html += '</div>';
	
	$(".sku-atom-list").eq(atom_index).append(html);
	$("#input-attrvalue").children().find("input").val();
	$(obj).prev().val('');
	$("#input-attrvalue").hide();
	
	//数组添加属性值
	if(attrArr["value"][atom_index] == undefined) {
		attrArr["value"][atom_index] = [attrValue];
	} else {
		attrArr["value"][atom_index][$(".sku-atom-list").eq(atom_index).children().length-1] = attrValue;
	}

	//生成表格
	$(".table-sku-stock").html(createTable(attrArr));
	attrtotalonbin();
}

//删除属性值
function rmAttrVal(obj, attr_index) {
	$(obj).parent().remove();
	var i;
	var index;
	for(i in attrArr["value"][attr_index]) {
		if(attrArr["value"][attr_index][i] == $(obj).prev().html()) {
			index = i;
		}
	}
	attrArr["value"][attr_index].splice(index, 1);
	//生成表格
	$(".table-sku-stock").html(createTable(attrArr));
	attrtotalonbin();
}
</script>
<!-- 添加属性操作 -->

<!-- 批量调价 -->
<script>
//批量填充数据
function showBatch(obj, type) {
	$("#input-batch").show().css({"top":$(obj).offset().top+$(obj).height(),"left":$(obj).offset().left-$("#input-attrvalue").width()/2+$(obj).width()/2});
	$("#batch-type").val(type);
}

//确认批量添加
function confirmBatch(obj) {
	//console.log($("#batch-type").val());
	switch($("#batch-type").val()) {
		case 'price': {
			var price = parseFloat($(obj).prev().val());
			$(".js-price").val(price);
		};break;
		case 'store': {
			var store = parseInt($(obj).prev().val());
			$(".js-stock-num").val(store);
			var total = 0;
			$(".js-stock-num").each(function () {
				total += parseInt($(this).val());
			});
			$("#total").val(total);
		};break;
	}
	$(obj).prev().val('');
	$("#input-batch").hide();
}

//隐藏添加框
function hideBatchPannel(obj) {
	$("#input-batch").hide();
}

//验证金钱
function isDecimal(txtValue, num, canNegative, event) {
    var whichCode = event.keyCode || event.which;
    if (whichCode == 8 || whichCode == 37 || whichCode == 39) {
        return true;
    }
    var key = String.fromCharCode(whichCode);
    var txt = txtValue.value;
    var numberString = txt.split('.');
    var pos = txt.indexOf('.');
    var inputPos = txtValue.value.length;
    if (key == '-') {
        if (inputPos > 0) {
            return false;
        }
    }
    if (key == '.') {
        if (inputPos == 0) {
            return false;
        }
    }
    if (pos > -1) {
        if (inputPos > pos) {
            var endstr = txt.substring(pos + 1);
            if (endstr.length >= num) {
                return false;
            }
        }
    }

    //不能输入两个以上.
    if (pos > -1 && key == '.') {
        return false;
    }
    //不能输入两个以上-
    pos = txt.indexOf('-');
    if (pos > -1 && key == '-') {
        return false;
    }
    if (canNegative) {
        objRegExp = /^(-)?[\d\.]*$/;
    }
    else {
        objRegExp = /^[\d\.]*$/;
    }
    return objRegExp.test(key);
}


</script>
<!-- 批量调价 -->
</body>

</html>